package com.gxa.controller;

import com.gxa.common.exception.RRException;
import com.gxa.entity.User;
import com.gxa.realm.OAuth2Token;
import com.gxa.service.UserService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api("用户接口")
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("登录接口")
    @RequestMapping("/user/login")
    public String login(User user, HttpSession session, HttpServletRequest request) {
//        if ("admin".equals(user.getUserName())) {
//            throw new RRException("test", 444);
//        }


        System.out.println("UserController--login--接受的参数 user" + user);


//        User user1 = this.userService.login(user.getUserName(), user.getPwd());
//        if(user1 != null){
//            //登录成功将用户信息放入session
//            session.setAttribute("currentUser",user1);
//            return "redirect:/main.html";
//        }else{
//            return "redirect:/index.html";
//        }
        //获取subject
//        Subject subject = SecurityUtils.getSubject();
//        AuthenticationToken token = new UsernamePasswordToken(user.getUserName(),user.getPwd());
//
//        try {//通过抛异常  表示登录失败
//            subject.login(token);
//            return "redirect:/main.html";
//        }catch (Exception e){
//            e.printStackTrace();
//            return "redirect:/index.html";
//        }

        Subject subject = SecurityUtils.getSubject();
        String token = request.getHeader("token");
        if (StringUtils.isBlank(token)) {
            token = request.getParameter("token");
        }
        System.out.println("前端传来的token------>" + token);

        AuthenticationToken oAuth2Token = new OAuth2Token(token);
        try {
            subject.login(oAuth2Token);
            return "redirect:/main.html";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/index.html";
        }
    }

    @ApiOperation("注销接口")
    @GetMapping("/user/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:../index.html";
    }

    @ApiOperation("注销接口01")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Authorization", value = "令牌", paramType = "header", required = true),
            @ApiImplicitParam(name = "username", value = "当前登录用户的账号，由后台从令牌中解析取得", required = true),
            @ApiImplicitParam(name = "planId", value = "考试计划编号", paramType = "path", required = true),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回考卷详细信息"),
            @ApiResponse(code = 306, message = "登录已超时，请重新登录"),
            @ApiResponse(code = 307, message = "令牌格式不正确"),
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "无效请求，本系统只支持AJAX请求"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @GetMapping("/user/logout01/{planId}")
    public String logout01(@ApiParam String username, @PathVariable("planId") String planId) {
        return "redirect:../index.html";
    }

}
